package com.wmx.test.transaction;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

public class TransactionProducer {
    public static void main(String[] args) {
        TransactionMQProducer producer = new TransactionMQProducer("transaction_group");
        producer.setNamesrvAddr("192.168.19.130:9876");
        for (int i = 1; i <= 2; i++) {
            try {
                String tags = "transaction" + i;
                String keys = "KEY" + i;
                byte[] body = ("Hello RocketMQ " + i).getBytes();
                Message msg = new Message("transaction_top", tags, keys, body);
                SendResult sendResult = producer.sendMessageInTransaction(msg,null);
                System.out.println(sendResult);
            } catch (MQClientException e) {
                e.printStackTrace();
            }
        }

        producer.shutdown();
    }
}
